// pages/shop/shop.js
import api from "../../utils/util"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    banners: ['https://s3.ax1x.com/2021/03/16/6yrX6A.jpg', 'https://s3.ax1x.com/2021/03/16/6yrOld.jpg', 'https://s3.ax1x.com/2021/03/16/6yrLSH.jpg'],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 2000,
    duration: 500,
    active: 0,
    categorys: []
  },

  onChange(event) {
    this.setData({
      active: event.detail
    });
    wx.switchTab({
      url: '/index'
    })
    wx.showToast({
      title: `点击标签 ${event.detail}`,
      icon: 'none',
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.getBanners();
    this.getCategorys();
  },

  async getCategorys(){
    const result = await api.get("/category/list?need=true");
    this.setData({
      categorys:result.data.slice(0,4)
    })
  },

  async getBanners() {
    const result = await api.get("/product/hot");
    this.setData({
      banners: result.data.records.map(e => e.productImgs)
    })
  },

  toCategory(e){
    const index = e.currentTarget.dataset['index'];
    const id = e.currentTarget.dataset['id'];
     app.globalData.category = {index:index,id:id};
     console.log(app.globalData.category);
    wx.switchTab({
      url: `/pages/category/category?index=${index}&id=${id}`,
    })
  },
  toDetail(e){
    const id = e.currentTarget.dataset['id'];
   wx.navigateTo({
     url: `/pages/productDetail/productDetail?id=${id}`,
   })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})